/*
 * Created on 21.07.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package ro.gateway.aida.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Person;
import ro.gateway.aida.obj.db.PersonDB;
import ro.gateway.aida.srv.EditorServlet;
import ro.gateway.aida.utils.HttpUtils;


/**
 * @author Mihai Postelnicu
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class ViewPersServlet extends EditorServlet {
    /* (non-Javadoc)
     * @see ro.gateway.aida.srv.EditorServlet#other_action_performed(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
     */
    protected void other_action_performed(HttpServletRequest request,
        HttpServletResponse response, String action)
        throws IOException, ServletException {
        // TODO Auto-generated method stub
    }

    /* (non-Javadoc)
     * @see ro.gateway.aida.srv.EditorServlet#confirm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void confirm(HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
        // TODO Auto-generated method stub
    }

    /* (non-Javadoc)
     * @see ro.gateway.aida.srv.EditorServlet#save(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void save(ServletContext application, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
        // TODO Auto-generated method stub
    }

    /* (non-Javadoc)
     * @see ro.gateway.aida.srv.EditorServlet#edit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void edit(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        // TODO Auto-generated method stub
    }

    public String getPNAME_BEANS() {
        return PNAME_BEANS;
    }

    public String getJSP_PAGE() {
        return JSP_PAGE;
    }

    protected void start_editing(ServletContext application,
        HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        super.start_editing(application, request, response);
		PersistenceToken token = PersistenceManager.tokenLookup( application );
        Hashtable bean = getBean(request);
        String sid = HttpUtils.getValidTrimedString(request, "id", null);

        if (request.getParameter("list") != null)
            request.setAttribute("mode", "list");

        if (sid == null)
            return;

        PersonDB persdb = PersonDB.getManager(token);
        Person pers = null;

        try {
            pers = persdb.getByID(Long.parseLong(sid));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        if (pers == null)
            return;

        beanSync(bean, "pers", pers);
    }

    public static final String PNAME_BEANS = "VIEWING_PERSONS";
    public static final String JSP_PAGE = "view.jsp";
}
